﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eleanor.BLL;

namespace Eleanor.Website.Utility.Filter
{
    public class HandleWwwSubdomainFilter : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var context = filterContext.HttpContext;
            if (context.Request.HttpMethod != "GET" || context.Request.RawUrl.Contains("/admin/"))
            {
                return;
            }
            if (SettingBLL.Instance.HandleWwwSubdomain())
            {
                var url = context.Request.Url.ToString();
                if (!url.Contains("://www."))
                {
                    url = context.Request.Url.ToString().Replace("://", "://www.");
                    context.Response.Clear();
                    context.Response.StatusCode = 301;
                    context.Response.AppendHeader("location", url);
                    context.Response.End();
                }
            }
       }
    }
}
